package com.jt.www.common.controller;

import com.jt.www.common.service.impl.FileServiceImpl;
import feign.Response;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author xq.wu03
 * @Classname FileController
 * @Description 文件上传下载
 * @create 2020-04-20 19:14
 */
@Api(value = "文件上传下载", tags = {"FILE-API"}, description = "上传下载")
@RestController
@RequestMapping(value = "/resNew")
public class FileController {

    public static final Logger log = LoggerFactory.getLogger(FileController.class);

    private static final String FREE_VALUES = "1,0";

    @Autowired
    private FileServiceImpl fileServiceImpl;

    @ApiOperation(value = "多文件上传方法", notes = "文件下载-实现")
    @PostMapping(value = "/batchupload", headers = {"content-type=multipart/form-data"})
    public String batchupload(@RequestParam(value = "files", required = true) @ApiParam(value = "文件列表", required = true) MultipartFile[] files,
                              @RequestParam(value = "applicationName", required = true) @ApiParam(value = "应用名称", required = true) String applicationName,
                              @RequestParam(value = "businessTypeCode", required = true) @ApiParam(value = "业务类型编码", required = true) String businessTypeCode,
                              @RequestParam(value = "free", required = true) @ApiParam(name = "free", value = "自由访问 1--是, 0--否", allowableValues = FREE_VALUES, required = true) String free) {
        return fileServiceImpl.batchupload(files, applicationName, businessTypeCode, free);
    }


    @ApiOperation(value = "文件下载", notes = "文件下载-实现")
    @ApiResponses({@ApiResponse(code = 200, response = String.class, message = "文件下载结果")})
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    @ResponseBody
    public Response downloadFileV3(HttpServletResponse response,
                                   @RequestParam(value = "fileId", required = true) @ApiParam(name = "fileId", value = "文件Id", required = true) String fileId,
                                   @RequestParam(value = "applicationName", required = false) @ApiParam(name = "applicationName", value = "应用名称") String applicationName,
                                   @RequestParam(value = "businessTypeCode", required = false) @ApiParam(name = "businessTypeCode", value = "业务类型编码") String businessTypeCode,
                                   @RequestParam(value = "businessDataTag", required = false) @ApiParam(name = "businessDataTag", value = "业务数据标签") String businessDataTag,
                                   @RequestParam(value = "free", required = true) @ApiParam(name = "free", value = "自由访问 1--是, 0--否", allowableValues = FREE_VALUES, required = true) String free) {
        return fileServiceImpl.downloadFile(fileId, applicationName, businessTypeCode, businessDataTag, free);
    }


}
